import { ref, nextTick } from 'vue'

export default function useCacheScrollTop(selectors: string) {
  const scrollTop = ref(0)

  const cacheScrollTop = () => {
    nextTick(() => {
      scrollTop.value = document.querySelector(selectors)?.scrollTop ?? 0
    })
  }

  const setScrollTop = () => {
    nextTick(() => {
      const element = document.querySelector(selectors)
      if (element !== null) {
        element.scrollTop = scrollTop.value
      }
    })
  }

  return {
    setScrollTop,
    cacheScrollTop
  }
}
